import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Community from '../views/Community.vue'
import Challenge from '../views/Challenge.vue'
import Carbon from '../views/Carbon.vue'
import Exchange from '../views/Exchange.vue'
import Guide from '../views/Guide.vue'
import Rewards from '../views/Rewards.vue'
import Profile from '../views/Profile.vue'
import ProfileEdit from '../views/ProfileEdit.vue'
import Publish from '../views/Publish.vue'
import Quiz from '../views/Quiz.vue'
import ItemDetail from '../views/ItemDetail.vue'
import Shop from '../views/Shop.vue'
import PostDetail from '../views/PostDetail.vue'
import UserAgreement from '../views/UserAgreement.vue'
import PrivacyPolicy from '../views/PrivacyPolicy.vue'
import LevelList from '../views/LevelList.vue'
import LevelQuiz from '../views/LevelQuiz.vue'
import QuizResult from '../views/QuizResult.vue'
import LevelDetail from '../views/LevelDetail.vue'
import UserStats from '../views/UserStats.vue'
import RandomQuiz from '../views/RandomQuiz.vue'
import AnswerQuestion from '../views/AnswerQuestion.vue'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { requiresAuth: false }
  },
  {
    path: '/',
    name: 'Home',
    component: Home,
    meta: { requiresAuth: true }
  },
  {
    path: '/community',
    name: 'Community',
    component: Community,
    meta: { requiresAuth: true }
  },
  {
    path: '/post/:id',
    name: 'PostDetail',
    component: PostDetail,
    meta: { requiresAuth: true }
  },
  {
    path: '/challenge',
    name: 'Challenge',
    component: Challenge,
    meta: { requiresAuth: true }
  },
  {
    path: '/carbon',
    name: 'Carbon',
    component: Carbon,
    meta: { requiresAuth: true }
  },
  {
    path: '/exchange',
    name: 'Exchange',
    component: Exchange,
    meta: { requiresAuth: true }
  },
  {
    path: '/guide',
    name: 'Guide',
    component: Guide,
    meta: { requiresAuth: true }
  },
  {
    path: '/rewards',
    name: 'Rewards',
    component: Rewards,
    meta: { requiresAuth: true }
  },
  {
    path: '/profile',
    name: 'Profile',
    component: Profile,
    meta: { requiresAuth: true }
  },
  {
    path: '/profile/edit',
    name: 'ProfileEdit',
    component: ProfileEdit,
    meta: { requiresAuth: true }
  },
  {
    path: '/publish',
    name: 'Publish',
    component: Publish,
    meta: { requiresAuth: true }
  },
  {
    path: '/quiz',
    name: 'Quiz',
    component: Quiz,
    meta: { requiresAuth: true }
  },
  {
    path: '/item/:id',
    name: 'ItemDetail',
    component: ItemDetail,
    meta: { requiresAuth: true }
  },
  {
    path: '/shop',
    name: 'Shop',
    component: Shop,
    meta: { requiresAuth: true }
  },
  {
    path: '/user-agreement',
    name: 'UserAgreement',
    component: UserAgreement,
    meta: { requiresAuth: false }
  },
  {
    path: '/privacy-policy',
    name: 'PrivacyPolicy',
    component: PrivacyPolicy,
    meta: { requiresAuth: false }
  },
  {
    path: '/level-list',
    name: 'LevelList',
    component: LevelList,
    meta: { requiresAuth: true }
  },
  {
    path: '/level-detail/:levelId',
    name: 'LevelDetail',
    component: LevelDetail,
    meta: { requiresAuth: true }
  },
  {
    path: '/level-quiz/:levelId',
    name: 'LevelQuiz',
    component: LevelQuiz,
    meta: { requiresAuth: true }
  },
  {
    path: '/quiz-result',
    name: 'QuizResult',
    component: QuizResult,
    meta: { requiresAuth: true }
  },
  {
    path: '/user-stats',
    name: 'UserStats',
    component: UserStats,
    meta: { requiresAuth: true }
  },
  { path: '/random-quiz', name: 'RandomQuiz', component: RandomQuiz, meta: { requiresAuth: true }},
  { path: '/answer-question', name: 'AnswerQuestion', component: AnswerQuestion, meta: { requiresAuth: true }},
  {
    path: '/:pathMatch(.*)*',
    redirect: '/'
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes: routes as any
})

// 路由守卫：检查登录状态
router.beforeEach((to, _from, next) => {
  // 检查localStorage和sessionStorage中的用户token
  const userToken = localStorage.getItem('userToken') || sessionStorage.getItem('userToken')
  
  // 如果页面需要登录但用户未登录，重定向到登录页
  if (to.meta.requiresAuth && !userToken) {
    next('/login')
  } 
  // 如果已登录用户访问登录页，重定向到首页
  else if (to.path === '/login' && userToken) {
    next('/')
  } 
  // 否则正常访问
  else {
    next()
  }
})

export default router
